row = JSON.parse($('#row').val());

function getValue(str) {
    if ($("#c-" + str).length > 0) {
        return $("#c-" + str).val();
    } else if ($("#c-" + str+"2").length > 0) {
        return $("#c-" + str+"2").val();
    } else if ($("#c-" + str+"3").length > 0) {
        return $("#c-" + str+"3").val();
    } else if ($("#c-" + str+"4").length > 0) {
        return $("#c-" + str+"4").val();
    } else if ($("#h-" + str).length > 0) {
        return $("#h-" + str).val();
    } else if (Config.admin[str] > 0) {
        return Config.admin[str];
    }
}
$(document).on("change change2", "#c-rel_type", function (e) {
    if ($('#c-rel_id').length > 0) {
        if ($("#c-rel_type").val() == 1) {
            if ($("#c-address_lv").length > 0) {
                $("#div-address_lv").parent("div.form-group").show();
            }
            if ($("#div-class_type_css").length > 0) {
                $("#div-class_type_css").hide();
            }
            if ($("#c-address_lv").length > 0) {
                $("#div-address_lv").show();
            }
            $.ajax({
                url: "/school/school/lists",
                data: {cityId: $("#city").val(), districtId: $("#county").val()},
                type: "GET",
                success: function (data) {
                    var $school = $('#c-rel_id');
                    $school.empty();
                    $school.append('<option value="">请选择学校</option>');
                    var list = JSON.parse(data);
                    $.each(list["list"], function (index, school) {
                        $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                    });
                    if (e.type == "change2") {
                        $school.val(row.rel_id);
                    }
                    $("#c-rel_id").trigger("change2");
                },
                error: function (xhr, status, error) {
                    console.error("Error fetching school list: ", error);
                }
            });
        } else if ($("#c-rel_type").val() == 2) {
            if ($("#c-address_lv").length > 0) {
                $("#div-address_lv").parent("div.form-group").hide();
            }
            if ($("#div-class_type_css").length > 0) {
                $("#div-class_type_css").show();
            }
            if ($("#c-address_lv").length > 0) {
                $("#div-address_lv").hide();
            }
            $.ajax({
                url: "/community/community/lists",
                data: {cityId: $("#city").val(), districtId: $("#county").val()},
                type: "GET",
                success: function (data) {
                    var $school = $('#c-rel_id');
                    $school.empty();
                    $school.append('<option value="">请选择社区</option>');
                    var list = JSON.parse(data);
                    $.each(list["list"], function (index, school) {
                        $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                    });
                    $school.val(row.rel_id);

                },
                error: function (xhr, status, error) {
                    console.error("Error fetching school list: ", error);
                }
            });
        }
    }
});

$("#c-rel_type").trigger("change2", function () {
    $("#c-rel_id").trigger("change2", function () {
        $("#c-pid").val(row.pid);
    });
});


$(document).on("change change2", "#c-rel_id", function (e) {
    if ($('#c-pid').length > 0) {
        var $school = $('#c-pid');
        $school.empty();
        $.ajax({
            url: "/addresslv/addresslv/lists",
            data: {relType: $("#c-rel_type").val(), relId: $("#c-rel_id").val(), cityId: $("#c-city_id").val()},
            type: "GET",
            success: function (data) {
                var list = JSON.parse(data);
                $school.empty();
                $school.append('<option value="">请选择层级</option>');
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                if (e.type == "change2") {
                    $("#c-pid").val(row.pid);
                }
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    }

    if ($('#c-address_lv').length > 0) {
        var $school = $('#c-address_lv');
        $school.empty();
        $school.append('<option value="">请选择父级</option>');
        $.ajax({
            url: "/addresslv/addresslv/lists",
            data: {relType: $("#c-rel_type").val(), relId: $("#c-rel_id").val(), cityId: $("#c-city_id").val()},
            type: "GET",
            success: function (data) {
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                $('#c-address_lv').val(row.address_lv);
                if ($("#c-pid").length > 0) {
                    $("#c-pid").trigger("change2");
                }
                if ($("#c-ocation_id").length > 0) {
                    $("#c-ocation_id").trigger("change2");
                }
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });

        if ($('#c-school_id').length > 0) {
            if ($("#c-community_id").length > 0) {
                $("#c-community_id").parents("div.form-group").hide();
            }
            if ($("#c-school_id").length > 0) {
                $("#c-school_id").parents("div.form-group").show();
            }
            if ($('#c-rel_type').length > 0 && $('#c-rel_type').val() == 1) {
                $('#c-school_id').val($('#c-rel_id').val());
            }
        }
        if ($('#c-community_id').length > 0) {
            if ($("#c-community_id").length > 0) {
                $("#c-community_id").parents("div.form-group").show();
            }
            if ($("#c-school_id").length > 0) {
                $("#c-school_id").parents("div.form-group").hide();
            }
            if ($('#c-rel_type').length > 0 && $('#c-rel_type').val() == 2) {
                $('#c-community_id').val($('#c-rel_id').val());
            }
        }

    }
});

$(document).on("change change2 change3 change4", "#c-address_lv,#c-addresslv_id,#c-address_lv3,#c-addresslv_id3,#c-addresslv_id4", function (e) {
    let cityId = getValue("city_id");
    let schoolId = getValue("school_id");
    let relType = getValue("rel_type");
    let relId = getValue("rel_id");
    let addressLv = getValue("addresslv_id");

    if (e.type == "change2") {
        $.ajax({
            url: "/addresslv/addresslv/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: cityId, school_id: schoolId, relType: relType, relId: relId,address_lv:addressLv},
            type: "GET",
            success: function (data) {
                var $school = $('#c-address_lv');
                var disabled = "";
                $school.empty();
                $school.append('<option value="">请选择学校</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    disabled = (school.haschild) ? " disabled  class='disabled'" : "";
                    $school.append('<option value="' + school.id + '"' + disabled + '>' + school.name + '</option>');
                });
                $('#c-address_lv').val(row.address_lv);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else if (e.type == "change3") {
        $.ajax({
            url: "/addresslv/addresslv/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: cityId, school_id: schoolId, relType: relType, relId: relId,address_lv:addressLv},
            type: "GET",
            success: function (data) {
                var $school = $('#c-address_lv,#c-addresslv_id,#c-address_lv3,#c-addresslv_id3');
                var disabled = "";
                $school.empty();
                $school.append('<option value="">请选择学校</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                if ($("#province_id").length > 0) {
                    $("#province_id").val(school.province_id);
                }
                if ($("#c-city_id").length > 0) {
                    $("#c-city_id").val(school.city_id);
                }
                if ($("#c-district_id").length > 0) {
                    $("#c-district_id").val(school.district_id);
                }
                if ($("#c-addresslv_id3").length > 0) {
                    $('#c-addresslv_id3').val(row.addresslv_id);
                }
                $school.val(row.address_lv);

            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else if (e.type == "change4") {
        $.ajax({
            url: "/addresslv/addresslv/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: cityId, school_id: schoolId, relType: relType, relId: relId,address_lv:addressLv},
            type: "GET",
            success: function (data) {
                var $school = $('#c-address_lv,#c-addresslv_id,#c-address_lv3,#c-addresslv_id3,#c-addresslv_id4');
                var disabled = "";
                $school.empty();
                $school.append('<option value="">请选择学校</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    disabled = (school.level>1) ? " disabled  class='disabled'" : "";
                    $school.append('<option value="' + school.id + '"'+disabled+'>' + school.name + '</option>');
                });
                if ($("#province_id").length > 0) {
                    $("#province_id").val(school.province_id);
                }
                if ($("#c-city_id").length > 0) {
                    $("#c-city_id").val(school.city_id);
                }
                if ($("#c-district_id").length > 0) {
                    $("#c-district_id").val(school.district_id);
                }
                if ($("#c-addresslv_id4").length > 0) {
                    $("#c-addresslv_id4").val(row.addresslv_id);
                }
                if ($("#c-subjecct-lv3-pid").length > 0) {
                    $("#c-subjecct-lv3-pid").trigger("change3");
                }
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else {
         $('#c-address_lv').val(row.address_lv);
        if ($("#c-pid").length > 0) {
            $("#c-pid").trigger("change2");
        }
        if ($("#c-ocation_id").length > 0) {
            $("#c-ocation_id").trigger("change2");
        }
        if ($("#c-subjecct-lv3-pid").length > 0) {
            $("#c-subjecct-lv3-pid").trigger("change3");
        }
    }
});

$(document).on("change change2", "#c-ocation_id", function (e) {
    if (e.type == "change2") {
        $.ajax({
            url: "/ocation/ocation/lists?XDEBUG_SESSION_START=54354",
            data: {
                cityId: Config.admin['city_id'],
                school_id: $("#c-school_id").val(),
                community_id: $("#c-community_id").val()
            },
            type: "GET",
            success: function (data) {
                var $school = $("#c-ocation_id");
                $school.empty();
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                $school.val(row.ocation_id);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else {

    }
});


if ($('select[name="row[province_id]"]').length > 0) {
    var row = JSON.parse($('#row').val());
    $.getJSON('/cityarea/cityarea/getAreas', {level: 1, parent_id: 0}, function (provinces) {
        var $province = $('select[name="row[province_id]"]');
        $province.append('<option value="">请选择省份</option>');
        $.each(provinces, function (index, province) {
            $province.append('<option value="' + province.id + '">' + province.name + '</option>');
        });
        $province.val(row.province_id);
        // 触发加载市级数据
        $province.change(function () {
            $.getJSON('/cityarea/cityarea/getAreas', {level: 2, parent_id: 1}, function (cities) {
                var $city = $('#city');
                var city_id = $city.val() ? $city.val() : row.city_id;
                $city.empty();
                $city.append('<option value="">请选择城市</option>');
                $city.append('<option value="0">区级</option>');
                $.each(cities, function (index, city) {
                    $city.append('<option value="' + city.id + '">' + city.name + '</option>');
                });
                $city.val(city_id);
                // 触发加载县级数据
                $city.change(function () {
                    var cityId = $(this).val() ? $(this).val() : row.city_id;
                    if (cityId == 0) {
                        $("#county").hide();
                        $county.empty();
                        return;
                    } else {
                        $("#county").show();
                    }
                    $.getJSON('/cityarea/cityarea/getAreas', {level: 3, parent_id: cityId}, function (counties) {
                        var $county = $('#county');
                        $county.empty();
                        $county.append('<option value="">请选择城区（县）</option>');
                        $county.append('<option value="0">市级</option>');
                        $.each(counties, function (index, county) {
                            $county.append('<option value="' + county.id + '">' + county.name + '</option>');
                        });
                        $county.val(row.district_id);
                    });
                });
                $city.trigger('change');
            });
        });
        $province.trigger('change');
    });
}


$(document).on("change change2", "#c-school_id", function (e) {
    if (e.type == "change2") {
        let cityId = getValue("city_id");
        let schoolId = getValue("school_id");
        $.ajax({
            url: "/school/school/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: cityId, school_id: schoolId},
            type: "GET",
            success: function (data) {
                var $school = $('#c-school_id');
                $school.empty();
                $school.append('<option value="">请选择学校</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                if (row.school_id) {
                    $school.val(row.school_id);
                    $("#c-pid").trigger("change2");
                }
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else {
        if ($("#c-pid").length > 0) {
            $("#c-pid").trigger("change2");
        }
    }
});

$(document).on("change change2", "#c-community_id", function (e) {
    if (e.type == "change2") {
        $.ajax({
            url: "/community/community/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: Config.admin['city_id'], school_id: Config.admin['school_id']},
            type: "GET",
            success: function (data) {
                var $school = $('#c-community_id');
                $school.empty();
                $school.append('<option value="">请选择学校</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                $school.val(row.community_id);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else {
        if ($("#c-pid").length > 0) {
            $("#c-pid").trigger("change2");
        }
    }
});

$(document).on("change change2 change3", "#c-subject_lv,#c-subject_lv2,#c-subject_lv3,#c-subjecct-lv3-pid", function (e) {
    let cityId = getValue("city_id");
    let schoolId = getValue("school_id");
    let addressLv = getValue("addresslv_id");
    let that = $(this);
    if (e.type == "change2") {
        $.ajax({
            url: "/subjectlv/subjectlv/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: cityId, school_id: schoolId, address_lv: addressLv},
            type: "GET",
            success: function (data) {
                var $school = that;
                $school.empty();
                $school.append('<option value="">请选择学科层级</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    disabled = (school.haschild) ? " disabled  class='disabled'" : "";
                    $school.append('<option value="' + school.id + '"' + disabled + '>' + school.name + '</option>');
                });
                $school.val(row.subject_lv);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else if (e.type == "change3") {
        $.ajax({
            url: "/subjectlv/subjectlv/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: cityId, school_id: schoolId,addresslv_id:addressLv},
            type: "GET",
            success: function (data) {
                var $school = that;
                $school.empty();
                $school.append('<option value="">请选择学科层级</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                $school.val(row.pid);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else {
        if ($("#c-pid").length > 0) {
            $("#c-pid").trigger("change2");
        }
    }
});

$(document).on("change change2,change3", "#c-pid,#c-subject_lv,#c-subject_lv2,#c-subject_lv2,#c-subjecct-lv3-pid", function (e) {
    let cityId = getValue("city_id");
    let schoolId = getValue("school_id");
    let addressLv = getValue("addresslv_id");
    debugger;
    let that = $(this);
    if (e.type == "change2") {
        $.ajax({
            url: "/subjectlv/subjectlv/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: Config.admin['city_id'], school_id: $("#c-school_id").val(), address_lv: addressLv},
            type: "GET",
            success: function (data) {
                var $school = $("#c-pid");
                $school.empty();
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    disabled = (school.haschild) ? " disabled  class='disabled'" : "";
                    $school.append('<option value="' + school.id + '"' + disabled + '>' + school.name + '</option>');
                });
                $school.val(row.pid);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    }else if (e.type == "change3") {
        debugger;
        $.ajax({
            url: "/subjectlv/subjectlv/lists?XDEBUG_SESSION_START=54354",
            data: {cityId: Config.admin['city_id'], school_id: $("#c-school_id").val(), address_lv: addressLv},
            type: "GET",
            success: function (data) {
                var $school = $("#c-pid,#c-subjecct-lv3-pid");
                $school.empty();
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                $school.val(row.pid);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else {
        if ($("#c-pid").length > 0) {
            $("#c-pid").trigger("change2");
        }
    }
});


$(document).on("change", "#group", function (e) {
    if ($("#group").val() == 8) {
        $.ajax({
            url: "/community/community/lists",
            data: {cityId: $("#city").val(), districtId: $("#county").val()},
            type: "GET",
            success: function (data) {
                var $school = $('#c-rel_id');
                $school.empty();
                $school.append('<option value="">请选择社区</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                $school.val(row.id);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    }

    if ($("#group").val() == 2) {
        $.ajax({
            url: "/school/school/lists",
            data: {cityId: $("#city").val(), districtId: $("#county").val()},
            type: "GET",
            success: function (data) {
                var $school = $('#c-rel_id');
                $school.empty();
                $school.append('<option value="">请选择学校</option>');
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    $school.append('<option value="' + school.id + '">' + school.name + '</option>');
                });
                $school.val(row.id);
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    }
});

if ($('#c-pid').length > 0 && $('#h-rel_type').length > 0 && $('#h-rel_id').length > 0) {
    var $school = $('#c-pid');
    $school.empty();
    $school.append('<option value="">请选择父级</option>');
    $.ajax({
        url: "/addresslv/addresslv/lists",
        data: {relType: $("#h-rel_type").val(), relId: $("#h-rel_id").val(), cityId: Config.cityId},
        type: "GET",
        success: function (data) {
            var list = JSON.parse(data);
            $.each(list["list"], function (index, school) {
                $school.append('<option value="' + school.id + '">' + school.name + '</option>');
            });
            $school.val(row.pid);
        },
        error: function (xhr, status, error) {
            console.error("Error fetching school list: ", error);
        }
    });
}

$(document).on("change change2", "#c-ocation_id2", function (e) {

    if (e.type == "change2") {
        $.ajax({
            url: "/ocation/ocation/lists2?XDEBUG_SESSION_START=54354",
            data: {
                cityId: Config.admin['city_id'],
                school_id: $("#c-school_id").val(),
                community_id: $("#c-community_id").val(),
                addresslv_id: $("#c-address_lv").val(),
                relType: getValue("rel_type"),
                relId: getValue("rel_id")
            },
            type: "GET",
            success: function (data) {
                var $school = $("#c-ocation_id2");
                $school.empty();
                var list = JSON.parse(data);
                $.each(list["list"], function (index, school) {
                    disabled = (school.haschild < 3) ? " disabled  class='disabled'" : "";
                    $school.append('<option value="' + school.id + '"' + disabled + '>' + school.name + '</option>');
                });
            },
            error: function (xhr, status, error) {
                console.error("Error fetching school list: ", error);
            }
        });
    } else {

    }
});

if ($("#c-school_id").length > 0) {
    $("#c-school_id").trigger("change2");
}

if ($('select[name="row[community_id]"]').length > 0) {
    $("select[name='row[community_id]']").trigger("change2");
}

if ($('#c-subject_lv2').length > 0) {
    $("#c-subject_lv2").trigger("change2");
}

if ($('#c-subject_lv3,#c-subjecct-lv3-pid').length > 0) {
    $("#c-subject_lv3").trigger("change3");
}

if ($('#c-ocation_id').length > 0) {
    $('#c-ocation_id').trigger("change2");
}


if ($('#c-classes_id').length > 0) {
    $('#c-classes_id').trigger("change2");
}

if ($('select[group]').length > 0) {
    $('select[group]').trigger("change3");
}

if ($('#c-address_lv').length > 0) {
    $('#c-address_lv').trigger("change2");
}

if ($('#c-address_lv3,#c-addresslv_id3').length > 0) {
    $('#c-address_lv3,#c-addresslv_id3').trigger("change3");
}

if ($('#c-address_lv4,#c-addresslv_id4').length > 0) {
    $('#c-address_lv4,#c-addresslv_id4').trigger("change4");
}

if ($('#c-subject_lv3,#c-subjecct-lv3-pid').length > 0) {
    $('#c-subject_lv3,#c-subjecct-lv3-pid').trigger("change3");
}